#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-08-21 11:54:21
# @Author  : wanglele (18911756410@163.com)
# @Link    : None
# @Version : "Python 3.7"

import os
import sys
import csv


class DataSave():
    '''
    这种保存方式的不足：只有爬虫程序正常完成后才能完整保存数据，需要进一步修改
    '''
    def __init__(self):
        self.datas = []

    def store_data(self, data):
        if data is None:
            return None
        self.datas.append(data)

    def dict_to_csv(self, filename):
        ''''''
        if len(self.datas) == 0:
            return
        with open('%s.csv' % filename, 'a') as filecsv:
            # 表头
            tab_header = [i for i in self.datas[0]]

            dict_write = csv.DictWriter(filecsv, tab_header)
            # 写入表头
            dict_write.writeheader()
            for data in self.datas:
                dict_write.writerow(data)
